{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Planning the Construction of homes\n", "On a housing estate on the Alicante coastline, two types of homes are being built: apartments and penthouses, whose prices are $p_1$ and $p_2$ M€ respectively. The curve of demand for apartments is $d_1 = 40 – 2p_1$ and $d_2 = 150 – 3p_2$ for penthouses.\n", "The builder calculated that, owing to the orders that he has already sent to his raw materials suppliers, it is worth building 15 times more apartments than penthouses. He has also calculated that building an apartment costs him 5M€, while a penthouse costs him 3M€. Knowing that the builder has a budget of 350 M€, work out the following:\n", "\n", "**a)** Write an NLP to calculate the optimal prices for apartments and penthouses.\n", "\n", "$\\max z = (p1-5)(40-2p_1) + (p_2-3)(150-3p_2) = -2p_1^2-3p_2^2+50p_1+159p_2-650$\n", "\n", "$\\text{s.t}$\n", "\n", "$5(40-2p_1)+3(150-3p_2) \\leq 350 \\rightarrow -10p_1-9p2+300 \\leq 0$\n", "\n", "$15(150-3p_2) \\leq (40-2p_1) \\rightarrow 2p_1-45p_2+2210 \\leq 0$\n", "\n", "$p_1, p_2 \\geq 0$\n", "\n", "\n", "**b)** Write the Kuhn Tucker conditions of the NLP problem:\n", "\n", "The Lagrangian is:\n", "\n", "$L = -2p_1^2-3p_2^2+50p_1+159p_2-650 + \\lambda_1\\left(-10p_1-9p2+300\\right) + \\lambda_2\\left(2p_1-45p_2+2210\\right)$\n", "\n", "**Gradient condition:**\n", "\n", "$\\nabla(L)=0$\n", "\n", "$\\dfrac{dL}{dq_1} = -4p_1+50-10\\lambda_1+2\\lambda_2 = 0$\n", "\n", "$\\dfrac{dL}{dq_2} = -6p_2+159-9\\lambda_1-45\\lambda_2 = 0$\n", "\n", "**Feasibility Condition:**\n", "\n", "$-10p_1-9p_2+300 \\leq 0$\n", "\n", "$2p_1-45p_2+2210 \\leq 0$\n", "\n", "**Orthogonality Condition:**\n", "\n", "$\\lambda_1·(-10p_1-9p2+300) = 0$\n", "\n", "$\\lambda_2·(2p_1-45p_2+2210) = 0$\n", "\n", "**Non-negativity condition:**\n", "\n", "$p_1, p_2 \\geq 0$\n", "\n", "$\\lambda_1, \\lambda_2 \\leq 0$\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "pycharm": { "name": "#%% \n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Optimization terminated successfully (Exit mode 0)\n", " Current function value: 2387450.0025050757\n", " Iterations: 3\n", " Function evaluations: 11\n", " Gradient evaluations: 3\n", "[1.10500000e+03 2.08014536e-08]\n" ] } ], "source": [ "import numpy as np\n", "from scipy.optimize import LinearConstraint, minimize\n", "\n", "def objective_func(p):\n", " return 2*p[0]**2 + 3*p[1]**2 - 50*p[0] - 159*p[1]+650\n", "\n", "rhs_coefs = np.array([[10, 9], [2, -45]])\n", "\n", "constraints = LinearConstraint(rhs_coefs,lb=[300, 2210], ub=[np.inf, np.inf])\n", "\n", "p0 = [5, 40]\n", "\n", "res = minimize(objective_func, p0, constraints=constraints, bounds=[(0, np.inf), (0, np.inf)], \n", " options={\"maxiter\":1000, \"disp\": True})\n", "\n", "print(res.x)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" }, "pycharm": { "stem_cell": { "cell_type": "raw", "source": [], "metadata": { "collapsed": false } } } }, "nbformat": 4, "nbformat_minor": 2 }